import { _decorator, Component, Node, Mesh, Vec3, utils, math, v3 } from 'cc';
import { Visualization } from './Visualization';;
const { ccclass, property, executeInEditMode } = _decorator;

/**
 * 根据半径做圆形辅助线
 */

@ccclass('CircleVisualization')
@executeInEditMode
export class CircleVisualization extends Visualization {

    @property
    private _radius: number = 1;
    @property
    public get radius(): number { return this._radius; }
    public set radius(v: number) { this._radius = v; this._modelChanged = true; }

    protected _updateMesh(mesh: Mesh) {
        let positions: number[] = [], colors: number[] = [];

        let color0 = [1, 1, 0, 1];

        function linkPoints(p0: Vec3, p1: Vec3, col: number[]) {
            if (!(p0 && p1)) return;
            Vec3.toArray(positions, p0, positions.length);
            Vec3.toArray(positions, p1, positions.length);
            colors.push(col[0], col[1], col[2], col[3]);
            colors.push(col[0], col[1], col[2], col[3]);
        }

        const count = 8;
        const ps = [];
        const stepT = Math.PI * 2 / count;
        for (let i = 0; i < count; i++) {
            let t = stepT * i;
            ps.push(v3(Math.cos(t) * this.radius, 0, Math.sin(t) * this.radius));
            if (i > 0) {
                linkPoints(ps[i - 1], ps[i], color0);
            }
        }
        linkPoints(ps[count - 1], ps[0], color0);

        return utils.MeshUtils.createMesh({ positions, colors }, mesh);
    }
}


